<template>
	<view class="content">
		<u-sticky>
			<view class="fafafa">
				<view class="padding">
					<!-- 顶部搜索栏 -->
					<view class="searchBox">
						<u-icon name="search" color="rgba(202, 202, 202, 1)" size="22"></u-icon>
						<input class="searchInput" placeholder-class="searchPlaceholder" type="text" placeholder="搜索关键字"
							v-model="keywords" @confirm="init" confirm-type="search">
					</view>
				</view>
				<!-- 商品 -->
				<view class="flexbetween nav">
					<view :class="current==1?'focustext':'nofocustext'" @click="selectabzh()">综合</view>
					<view class="flexleft">
						<view :class="current==2?'focustext ':'nofocustext'" @click="selectab()">销量</view>
						<view class="trbox margin_left1">
							<view :class="type==1?'shanghjt':'shanghuijt'"></view>
							<view :class="type==2?'xiahjt':'xiahuijt'"></view>
						</view>
					</view>
					<view class="flexleft">
						<view :class="current==3?'focustext':'nofocustext'" @click="selectabprice()">价格</view>
						<view class="trbox margin_left1">
							<view :class="type==3?'shanghjt':'shanghuijt'"></view>
							<view :class="type==4?'xiahjt':'xiahuijt'"></view>
						</view>
					</view>
				</view>
			</view>
		</u-sticky>
		<view class="flexbetween flex_wrap paddinglr">
			<view class="shangpbox margin_top border1" v-for="item in list.data" :key="item.id"
				@click="goGoodsInfo(item.id)">
				<view class="shangptp">
					<image :src="item.image_text" mode="widthFix"></image>
				</view>
				<view class="padding15 ffffff baibox margin_top1">
					<view class="yihang text_a color_san">{{item.name}}</view>
					<view class="flexleft margin_top2">
						<view class="word_hh orange margin_right2">￥{{item.price}}</view>
						<view class="text_pee color_jiu">
							已拼{{item.sale_num}}件
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="flexcenter placeholderBoxTop" v-if="!list.data.length">
			<image class="placeholderBox" src="/static/placeholderimg.png" mode="widthFix"></image>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				current: 1,
				type: 0, // 1销量由高到底，2销量由低到高，3价格由高到底，4价格由低到高
				page: 1,
				keywords: "",
				category_id: "", //分类id
				list: {
					data: []
				}
			}
		},
		onLoad(options) {
			if (options.keywords) {
				this.keywords = options.keywords
			}
			this.init()
		},
		methods: {
			init(isPage, page) {
				uni.showLoading({
					mask:true,
					title:"请稍后"
				})
				let _this = this;
				let data = {
					category_id: this.category_id, //分类id
					page: page || 1,
					keyword: this.keywords,
					sort_type: this.current,
					weigh: this.type % 2 == 0 ? 1 : 2
				}
				let url = "/api/goods/getlist"
				httpRequest.request(url, 'GET', data, false, false, true).then(res => {
					uni.hideLoading()
					if (res.code == 1) {
						let resList = res.data;
						let dataList = _this.list;
						if (isPage == true) {
							_this.list.data = dataList.data.concat(resList.data);
						} else {
							this.list = res.data
							_this.list.data = resList.data;
						}
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			onReachBottom() {
				let _this = this;
				let list = _this.list;
				let page = _this.page;
				if (list.last_page <= page) {
					return false;
				}
				page++;
				_this.page = page;
				_this.init(true, page);
			},
			goGoodsInfo(id) {
				uni.navigateTo({
					url: "/pages_index/goodsInfo?id=" + id
				})
			},
			// 切换筛选项
			selectabzh() {
				this.current = 1
				this.type = 0
				this.init()
			},
			selectab() {
				this.current = 2
				this.type = this.type == 0 ? 1 : this.type == 1 ? 2 : 1
				this.init()
			},
			selectabprice() {
				this.current = 3
				this.type = this.type != 3 ? 3 : 4
				this.init()
			},
		}
	}
</script>
<style lang="scss" scoped>
	.searchBox {
		padding: 18rpx 30rpx;
		background: #F3F3F3;
		border-radius: 60rpx;
		display: flex;
		align-items: center;
		flex: 1;

		.searchInput {
			margin-left: 20rpx;
			flex: 1;
			font-size: 24rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
		}

		.searchPlaceholder {
			font-size: 24rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #C5C5C5;
		}
	}

	.orange {
		font-size: 24rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
		color: #F3271C;
	}

	.shilitu {
		width: 86rpx;
		height: 86rpx;
		border-radius: 8rpx;
 overflow: hidden;
		image {
			width: 100%;
			height: 100%;
			border-radius: 8rpx;
		}
	}

	.liji {
		padding: 0rpx 20rpx;
	}

	.duihuan {
		text-align: center;
		padding: 12rpx 0rpx;
		background-color: RGBA(255, 117, 16, 1);
		border-radius: 42rpx;
		font-size: 24rpx;
		font-weight: 500;
		color: #FFFFFF;
	}

	.nav {
		padding: 30rpx 100rpx;
	}

	// 商品列表样式
	.shangpbox {
		width: 48%;
		background-color: #FFFFFF;
		padding-bottom: 20rpx;
	}

	.baibox {
		border-radius: 0 0 11rpx 11rpx;
	}

	.shangptp {
		width: 100%;
		height: 338rpx;
		border-radius: 11rpx;
		overflow: hidden;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.xiahuijt {
		margin-top: 6rpx;
		width: 13rpx;
		height: 10rpx;
		background-image: url('/static/image/home/huixia.png');
		background-size: 100% 100%;
	}

	.shanghuijt {
		width: 13rpx;
		height: 10rpx;
		background-image: url('/static/image/home/xiabiao.png');
		background-size: 100% 100%;
	}

	.shanghjt {
		width: 13rpx;
		height: 10rpx;
		background-image: url('/static/image/home/redshang.png');
		background-size: 100% 100%;
	}

	.xiahjt {
		margin-top: 6rpx;
		width: 13rpx;
		height: 10rpx;
		background-image: url('/static/image/home/shangbiao.png');
		background-size: 100% 100%;
	}

	.nofocustext {
		font-size: 26rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #666666;
	}

	.focustext {
		font-size: 30rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
		color: #000000;
	}

	.sousuobox {
		width: 100%;
		background: #F3F3F3;
		padding: 20rpx 40rpx;
		border-radius: 60rpx;
		box-sizing: border-box;
	}
</style>